package hot;


/**
 * @author lakersUp
 * @Classname
 * @Description
 * @Date 2022/3/12 9:10
 * @Version 1.0
 */
 class TreeNode {
      int val;
     TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
public class Hot142maxPathSum {
    int max=Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        dfs(root);
        return max;
    }
    private int dfs(TreeNode root) {
        if(root==null){
            return 0;
        }
        int left=dfs(root.left);
        int right=dfs(root.right);
        left=left>0?left:0;
        right=right>0?right:0;
        max=Math.max(max,left+right+root.val);
        return root.val+Math.max(left,right);
    }
}
